package com.icss.ah.coupons.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CouponsService {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	/**
	 * 优惠卷列表查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getCouponsList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("CouponsBean.getCouponsList",map);
			infolog.info("CouponsService|getCouponsList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|getCouponsList", e);
		}
		return list;
	}
	
	/**
	 * 优惠卷统计
	 * @param map
	 * @return
	 */
	public Long getCouponsListCount(Map<String, String> map){
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("CouponsBean.getCouponsListCount",map);
			infolog.info("CouponsService|getCouponsListCount|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|getCouponsListCount", e);
		}
		return count;
	}
	
	/**
	 * 优惠卷保存
	 * @param map
	 */
	public void saveInfo(Map<String, String> map){
		try {
			sqlSessionTemplate.insert("CouponsBean.saveInfo",map);
			infolog.info("CouponsService|saveInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|saveInfo", e);
		}
	}
	
	/**
	 * 优惠卷编辑
	 * @param map
	 */
	public void updateInfo(Map<String, String> map){
		try {
			sqlSessionTemplate.update("CouponsBean.updateInfo",map);
			infolog.info("CouponsService|updateInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|updateInfo", e);
		}
	}
	
	/**
	 * 优惠卷状态启用/禁用
	 * @param cpsId
	 */
	public void updateStatus(String cpsId){
		try {
			sqlSessionTemplate.update("CouponsBean.updateStatus",cpsId);
			infolog.info("CouponsService|updateStatus");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|updateStatus", e);
		}
	}
	/**
	 * 优惠卷删除
	 * @param cpsId
	 */
	public boolean deleteInfo(HashMap map){
		boolean result = false;
		try {
			sqlSessionTemplate.delete("CouponsBean.deleteInfo",map);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|deleteInfo", e);
		}
		return result;
	}
	
	/**
	 * 优惠卷详情
	 * @param cpsId
	 * @return
	 */
	public Map<String, Object> getCouponsInfo(String cpsId){
		Map<String, Object> info = new HashMap<String, Object>();
		try {
			info = sqlSessionTemplate.selectOne("CouponsBean.getCouponsInfo",cpsId);
			infolog.info("CouponsService|getCouponsInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CouponsService|getCouponsInfo", e);
		}
		return info;
	}

}
